package com.imooc.controller;

import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UsersVO;
import com.imooc.resource.FileResource;
import com.imooc.service.FdfsService;

import com.imooc.service.center.CenterUserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;

import com.imooc.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@RestController
@RequestMapping("fdfs")
public class CenterUserController extends BaseController {


    @Autowired
    private FdfsService fdfsService;

    @Autowired
    private FileResource fileResource;

    @Autowired
    private CenterUserService centerUserService;


    @PostMapping("uploadFace")
    public IMOOCJSONResult uploadFace(
             String userId,
            MultipartFile file,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        String path = "";
        // .sh .php
        // 开始文件上传
        if (file != null) {
                // 获得文件上传的文件名称
                String fileName = file.getOriginalFilename();
                if (StringUtils.isNotBlank(fileName)) {
                    // 文件重命名  imooc-face.png -> ["imooc-face", "png"]
                    String fileNameArr[] = fileName.split("\\.");

                    // 获取文件的后缀名
                    String suffix = fileNameArr[fileNameArr.length - 1];

                    // 图片格式的限制
                    if (!suffix.equalsIgnoreCase("png") &&
                            !suffix.equalsIgnoreCase("jpg") &&
                            !suffix.equalsIgnoreCase("jpeg")) {
                        return IMOOCJSONResult.errorMsg("图片格式不正确！");
                    }
                    // fastDFS文件上传
                     //path = fdfsService.upload(file,suffix);
                    // 阿里云OSS文件上传
                    path = fdfsService.uploadOSS(file,userId,suffix);
                    System.out.println(path);
                }
        } else {
            return IMOOCJSONResult.errorMsg("文件不能为空！");
        }

        if(StringUtils.isNotBlank(path)){
            // 不需要时间戳了，每次上传fastdfs所赋予的文件名都是不同的
            //String finalUserFaceUrl = fileResource.getHost() + path;
            // 阿里云OSS 图片存储地址
            String finalUserFaceUrl = fileResource.getOssHost() +path;

            // 更新用户头像到数据库
            Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl);

            //userResult = setNullProperty(userResult);
            // 增加令牌token，会整合进redis，分布式会话
            UsersVO usersVO = conventUsersVO(userResult);
            CookieUtils.setCookie(request, response, "user",
                    JsonUtils.objectToJson(usersVO), true);
            return IMOOCJSONResult.ok();
        }else{
            return IMOOCJSONResult.errorMsg("上传头像失败!");
        }


    }



}
